package com.yc.starter.login.web;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.yc.starter.login.LoginProperties;
import com.yc.starter.login.biz.AuthBiz;
import com.yc.starter.login.biz.BizException;
import com.yc.starter.login.util.JwtUtils;
import com.yc.starter.login.util.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("auth")
public class LoginAction {

    @Autowired
    LoginProperties loginProperties;

    @RequestMapping("props")
    public LoginProperties properties(){
        return loginProperties;
    }

    @Autowired
    AuthBiz  authBiz;

    @RequestMapping("login")
    Object login(String username, String password,
                 HttpSession session,
                 HttpServletResponse response) throws BizException{
        try{
            Object ret = authBiz.login(username, password);
            // 登录成功, 保存用户对象到会话
            session.setAttribute(loginProperties.getSessionKey(),
                    ret);
            // 登录成功, 生成JWT
            String jwt = null;
            if(ret instanceof Map){
                jwt = JwtUtils.createJWT(30, (Map<String, Object>) ret);
            } else {
                // 用户实体类对象 转为 JSON 然后再转为 Map
                ObjectMapper objectMapper = new ObjectMapper();
                Map<String, Object> map = objectMapper.convertValue(ret, HashMap.class);
                jwt = JwtUtils.createJWT(30, map);
            }
            // 登录成功, 把JWT返回给客户端
            response.addHeader("Authorization", "Bearer " + jwt);

            return Map.of("msg","登录成功",
                    "code", 1,
                    "data", ret);
        }catch (BizException e){
            e.printStackTrace();
            return Map.of("msg",e.getMessage(),
                    "code", 0);
        }
    }

    @RequestMapping("myinfo")
    public Object myinfo(HttpSession session){
        Object ret = session.getAttribute(loginProperties.getSessionKey());
        if(ret == null){
            return Map.of("msg","未登录",
                    "code", 0);
        }
        return Map.of("msg","查询成功",
                "code", 1,
                "data", ret);
    }

    @RequestMapping("logout")
    public Object logout(HttpSession session){
        session.invalidate();
        return Map.of("msg","退出成功",
                "code", 1);
    }

    @RequestMapping("forgetPwd")
    public Object forgetPwd(String username, HttpSession session){
        try{
            String code = authBiz.forgetPwd(username);
            session.setAttribute("forgetVcode",code);
            session.setAttribute("forgetUsername",username);
            return Map.of("msg","验证码发送成功",
                    "code", 1);
        } catch (BizException e){
            return Map.of("msg",e.getMessage(),
                    "code", 0);
        }
    }

    @RequestMapping("resetPwd")
    public Object resetPwd(String pwd, String repwd,
                           String vcode,HttpSession session){
        try{
            String username = (String) session.getAttribute("forgetUsername");
            String scode = (String) session.getAttribute("forgetVcode");
            Utils.checkEmpty(vcode, "验证码不能为空")
                    .check(!scode.equals(vcode), "验证码错误");
            authBiz.resetPwd(username, pwd, repwd);
            return Map.of("msg","密码重置成功",
                    "code", 1);
        } catch (BizException e){
            return Map.of("msg",e.getMessage(),
                    "code", 0);
        }
    }

}
